package com.bw.find_module.recommend;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.entity.RoomUserEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.provider.LoginProvider;
import com.bw.find_module.entity.RecommendEntity;
import com.bw.find_module.entity.hasattent.HasEntity;
import com.bw.find_module.find.FindModel;
import com.bw.network_sdk.entity.BaseEntity;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RecommendViewModel extends BaseViewModel<FindModel> {

    @Autowired
    LoginProvider loginProvider;

    private int userId = -1;
    public void onHas(int tagId){
        mModel.requestHas(tagId,userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void initData() {
        ARouter.getInstance().inject(this);
        loginProvider.getUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void onRestle(BaseEntity entity) {
        if (entity instanceof RecommendEntity) {
            RecommendEntity recommendEntity = (RecommendEntity) entity;
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.START_VIEW_MODEL;
            Map<LiveDataEntity.LiveDataType, Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY, recommendEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }else if (entity instanceof HasEntity){
            HasEntity hasEntity = (HasEntity) entity;
            LiveDataEntity liveDataEntity = new LiveDataEntity();
            liveDataEntity.action = LiveDataEntity.LiveDataType.IS_ATTENTION;
            Map<LiveDataEntity.LiveDataType, Object> map = new HashMap<>();
            map.put(LiveDataEntity.LiveDataType.ENTITY, hasEntity);
            liveDataEntity.data = map;
            liveDataViewModel.post(liveDataEntity);
        }else if (entity instanceof RoomUserEntity){
            RoomUserEntity roomUserEntity = (RoomUserEntity) entity;
            this.userId = roomUserEntity.data.data.userId;
            mModel.requestFind(0,10,0,"all",roomUserEntity.data.data.userId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }
}
